package com.medicine.hbp.ui;

import java.util.List;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.ClassificationDetailAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.sp.DadosSp;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;

/**
 * 首页标签类目详情
 *
 * @author jindandan
 */
public class ClassificationDetailActivity extends BaseActivity implements IXListViewListener {
	@BindView(id = R.id.num01, click = false)public TextView num01;
	@BindView(id = R.id.num02, click = false)public TextView num02;
	@BindView(id = R.id.ll_detail, click = true)public LinearLayout ll_detail;
	@BindView(id = R.id.txt_detail, click = false)public TextView txt_detail;
	private XListView listView;
	private ClassificationDetailAdapter adapter;
	private ImageView image_no_wifi;

	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	/**
	 *  排序(不传默认为 0) 综合排序 0 好评数最多
	 *  最新排序 1 时间最近
	 *  最热排序 2 评论数最多
	 */
	private int sort=0;
	private List<artGroupVo> artGroupVos;
	private KJBitmap bitmap = new KJBitmap();

	private String name;
	private String id;
	private String numone;
	private String numtwo;

	private String Status;
	private String Url;

	@Override
	public void setRootView() {
		super.setRootView();
		name=getIntent().getStringExtra("name");
		id=getIntent().getStringExtra("id");
		numone=getIntent().getStringExtra("numone");
		numtwo=getIntent().getStringExtra("numtwo");

		Status=getIntent().getStringExtra("Status");
		Url=getIntent().getStringExtra("Url");
		setContentView(R.layout.activity_classification_detail);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		initTop();
		num01.setText(numone);
		num02.setText(numtwo);
		if(!TextUtils.isEmpty(Status)&&Status.equals("Y")){
			txt_detail.setText(name+"详情");
			ll_detail.setVisibility(View.VISIBLE);
		}
		listView = (XListView) findViewById(R.id.xListView_detail);
		image_no_wifi = (ImageView) findViewById(R.id.image_no_wifi);
		adapter = new ClassificationDetailAdapter(aty);
		listView.setAdapter(adapter);
		listView.setXListViewListener(this);
		adapter.notifyDataSetChanged();
		DadosSp.setAdapter(aty, id, adapter);
		if(adapter.getCount()==0){
			if(NetWorkUtils.getAPNType(aty)==0){
				listView.setVisibility(View.GONE);
				image_no_wifi.setVisibility(View.VISIBLE);
			}else{
				listView.setVisibility(View.VISIBLE);
				image_no_wifi.setVisibility(View.GONE);
				httpArtGroupList();
			}
		}else{
			listView.setVisibility(View.VISIBLE);
			image_no_wifi.setVisibility(View.GONE);
			httpArtGroupList();
		}
		image_no_wifi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(NetWorkUtils.getAPNType(aty)==0){
					listView.setVisibility(View.GONE);
					image_no_wifi.setVisibility(View.VISIBLE);
					ToastUtil.showShort(aty, "还没有网络哦");
				}else{
					listView.setVisibility(View.VISIBLE);
					image_no_wifi.setVisibility(View.GONE);
					httpArtGroupList();
				}
			}
		});
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.ll_detail:
				Intent intent=new Intent(aty,BannerWebActivity.class);
				intent.putExtra("URL",Url);
				startActivity(intent);
				break;
			default:
				break;
		}
	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle(name);
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		topview.setRightVisibility(View.INVISIBLE);
	}

	/**
	 * 标签内部文章组获取
	 */
	private void httpArtGroupList() {
		HttpParams params = new HttpParams();
		params.put("laberId", id);
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(this,HttpInterface.ART_GROUP_LIST, params,
				new AppHttpCallBack<artGroupListVo>(artGroupListVo.class, aty) {
					@Override
					public void onSuccess(artGroupListVo result) {
						boolean hasMore = true;
						artGroupVos = result.artInfo;
						if (refresh) {
							DadosSp.setClassificationDetail(aty, id, gson.toJson(result));
							adapter.changeData(result.artInfo);
						} else {
							adapter.addItems(result.artInfo);
							adapter.notifyDataSetChanged();
						}
						if (result.artInfo.size() < 1) {
							hasMore = false;
						}
						listView.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						listView.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});

	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		listView.setPullLoadEnable(false);
		httpArtGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpArtGroupList();
	}
}
